package com.godpaper.the2tigers.busniess.managers
{
	import com.godpaper.the2tigers.model.BoardModel;
	import com.godpaper.the2tigers.model.PiecesModel;
	import com.godpaper.the2tigers.model.PlayerModel;
	import com.godpaper.the2tigers.util.PositionTransactUtil;
	import com.godpaper.the2tigers.views.components.GasketButton;
	import com.godpaper.the2tigers.views.components.PieceButton;
	import com.godpaper.the2tigers.vo.PieceVO;
	
	import de.polygonal.ds.Array2;
	import de.polygonal.ds.GraphNode;
	
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;

	/**
	 * 
	 * Manager Pieces' behavior
	 * 
	 */	
	public class PiecesManager
	{
		public function PiecesManager()
		{
		}
		
		//simulation pieceButtons such as ToggleButtonBar;
		public function untoggleOtherPieces(nameFilter:String):void
		{
			var myPieceButton:PieceButton; 
			for(var i:int=0;i<PiecesModel.getInstance().bluePiecesCollection.length;i++)
			{
				myPieceButton = PiecesModel.getInstance().bluePiecesCollection.getItemAt(i) as PieceButton; 
				if(myPieceButton.pieceVO.name!=nameFilter)
				{
					myPieceButton.selected = false;
				}
			}
		}
		//depth-first
		public static var depthFirstVisitedFunc:Function = function(node:GraphNode):Boolean
		{
			return true;
		}
		//breadth-first
		private static var breadthFirstVisitedFunc:Function = function(node:GraphNode):Boolean
		{
		    return true;
		}
		
		public function untoggleOtherGaskets(currentPosition:Array):void
		{
			//refresh walkableGBs.
			BoardModel.getInstance().walkableGBs.removeAll();
			//first off: untoggle other gasketbuttons;
			for(var i:int=0;i<PiecesModel.getInstance().gasketCollection.length;i++)
			{
				(PiecesModel.getInstance().gasketCollection.getItemAt(i)  as GasketButton).enabled = false;
			}
			//todo
			//just simple check sideby piecesbutton
			//and calculate the tigers' addressable gasketsbutton position; 
			var currentGB:GasketButton = PiecesModel.getInstance().gasketCollection.getItemAt( PositionTransactUtil.transactArray2Index(currentPosition))
				as GasketButton;
			trace("-------------currentGasketButton neighbourship list begin-----------");	
			//left
			if(currentGB.left)
			{
				var leftPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]-1,currentPosition[1] ) as ArrayCollection;
				trace("leftPBs:",leftPBs);
				if(leftPBs==null)
				{
					currentGB.left.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.left);
				}else
				{
					if(leftPBs.length>0)
					{
						//todo: propose tiger jump walkable gasketButtons
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (leftPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1 
								&& currentGB.left.left!=null)
							{
								var leftLeftPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]-2,currentPosition[1] ) as ArrayCollection;
								trace("leftLeftPBs:",leftLeftPBs);
								if(leftLeftPBs==null)
								{
									trace("currentGB.left.left:",currentGB.left.left);
									currentGB.left.left.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.left.left);
								}
							}
						}
					}
				}
			}
			//top
			if(currentGB.top)
			{
				var topPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0],currentPosition[1]-1 ) as ArrayCollection;
				trace("topPBs:",topPBs);
				if(topPBs==null)
				{
					currentGB.top.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.top);
				}else
				{
					if(topPBs.length>0)
					{
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (topPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.top.top!=null)
							{
								var topTopPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0],currentPosition[1]-2 ) as ArrayCollection;
								trace("topTopPBs:",topTopPBs);
								if(topTopPBs==null)
								{
									trace("currentGB.top.top:",currentGB.top.top);
									currentGB.top.top.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.top.top);
								}
							}
						}	
					}
				}
			}
			//right
			if(currentGB.right)
			{
				var rightPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]+1,currentPosition[1] ) as ArrayCollection;
				trace("rightPBs:",rightPBs);
				if(rightPBs==null)
				{
					currentGB.right.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.right);
				}else
				{
					if(rightPBs.length>0)
					{
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (rightPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.right.right!=null)
							{
								var rightRightPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]+2,currentPosition[1] ) as ArrayCollection;
								trace("rightRightPBs:",rightRightPBs);
								if(rightRightPBs==null)
								{
									trace("currentGB.right.right:",currentGB.right.right);
									currentGB.right.right.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.right.right);
								}
							}
						}	
					}
				}
			}
			//bottom
			if(currentGB.bottom)
			{
				var bottomPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0],currentPosition[1]+1 ) as ArrayCollection;
				trace("bottomPBs:",bottomPBs);
				if(bottomPBs==null)
				{
					currentGB.bottom.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.bottom);
				}else
				{
					if(bottomPBs.length>0)
					{
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (bottomPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.bottom.bottom!=null)
							{
								var bottomBottomPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0],currentPosition[1]+2 ) as ArrayCollection;
								trace("bottomBottomPBs:",bottomBottomPBs);
								if(bottomBottomPBs==null)
								{
									trace("currentGB.bottom.bottom:",currentGB.bottom.bottom);
									currentGB.bottom.bottom.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.bottom.bottom);
								}
							}
						}	
					}
				}
			}
			//upperLeft
			if(currentGB.upperLeft)
			{
				var upperLeftPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]-1,currentPosition[1]-1) as ArrayCollection;
				trace("upperLeftPBs:",upperLeftPBs);
				if(upperLeftPBs==null)
				{
					currentGB.upperLeft.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.upperLeft);
				}else
				{
					if(upperLeftPBs.length>0)
					{
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (upperLeftPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.upperLeft.upperLeft!=null)
							{
								var upperLeftUpperLeftPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]-2,currentPosition[1]-2) as ArrayCollection;
								trace("upperLeftUpperLeftPBs:",upperLeftUpperLeftPBs);
								if(upperLeftUpperLeftPBs==null)
								{
									trace("currentGB.upperLeft.upperLeft:",currentGB.upperLeft.upperLeft);
									currentGB.upperLeft.upperLeft.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.upperLeft.upperLeft);
								}
							}
						}
					}
				}
			}
			//lowerLeft
			if(currentGB.lowerLeft)
			{
				var lowerLeftPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]-1,currentPosition[1]+1) as ArrayCollection;
				trace("lowerLeftPBs:",lowerLeftPBs);
				if(lowerLeftPBs==null)
				{
					currentGB.lowerLeft.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.lowerLeft);
				}else
				{
					if( lowerLeftPBs.length>0)
					{
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (lowerLeftPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.lowerLeft.lowerLeft!=null)
							{
								var lowerLeftLowerLeftPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]-2,currentPosition[1]+2) as ArrayCollection;
								trace("lowerLeftLowerLeftPBs:",lowerLeftLowerLeftPBs);
								if(lowerLeftLowerLeftPBs==null)
								{
									trace("currentGB.lowerLeft.lowerLeft:",currentGB.lowerLeft.lowerLeft);
									currentGB.lowerLeft.lowerLeft.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.lowerLeft.lowerLeft);
								}
							}
						}
					}
				}
			}
			//lowerRight
			if(currentGB.lowerRight)
			{
				var lowerRightPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]+1,currentPosition[1]+1) as ArrayCollection;
				trace("lowerRightPBs:",lowerRightPBs);
				if(lowerRightPBs==null)
				{
					currentGB.lowerRight.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.lowerRight);
				}else
				{
					if( lowerRightPBs.length>0 )
					{
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (lowerRightPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.lowerRight.lowerRight!=null)
							{
								var lowerRightLowerRightPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]+2,currentPosition[1]+2) as ArrayCollection;
								trace("lowerRightLowerRightPBs:",lowerRightLowerRightPBs);
								if(lowerRightLowerRightPBs==null)
								{
									trace("currentGB.lowerRight.lowerRight:",currentGB.lowerRight.lowerRight);
									currentGB.lowerRight.lowerRight.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.lowerRight.lowerRight);
								}
							}
						}
					}
				}
			}
			//upperRight
			if(currentGB.upperRight)
			{
				var upperRightPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]+1,currentPosition[1]-1) as ArrayCollection;
				trace("upperRightPBs:",upperRightPBs);
				if(upperRightPBs==null)
				{
					currentGB.upperRight.enabled = true;
					BoardModel.getInstance().walkableGBs.addItem(currentGB.upperRight);
				}else
				{
					if(upperRightPBs.length>0 )
					{
						if( PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW )
						{
							if( (upperRightPBs.getItemAt(0) as PieceButton).name.indexOf(PieceButton.NAME_PREFIX_BLUE)!=-1
								&& currentGB.upperRight.upperRight!=null)
							{
								var upperRightUpperRightPBs:ArrayCollection = BoardModel.getInstance().gamePosition.gett( currentPosition[0]+2,currentPosition[1]-2) as ArrayCollection;
								trace("upperRightUpperRightPBs:",upperRightUpperRightPBs);
								if(upperRightUpperRightPBs==null)
								{
									trace("currentGB.upperRight.upperRight:",currentGB.upperRight.upperRight);
									currentGB.upperRight.upperRight.enabled = true;
									BoardModel.getInstance().walkableGBs.addItem(currentGB.upperRight.upperRight);
								}
							}
						}
					}
				}
			}
			trace("-------------currentGasketButton neighbourship list end-----------");	
			trace("currentGB's walkableGBs:",BoardModel.getInstance().walkableGBs.toString());
		}
		
		
		public static function tryMovement(target:PieceButton,newDestination:Point,newPosition:Array):Boolean
		{
			//trace("target name: ",target.pieceVO.name,"destination original point: ",destination.x,destination.y);
			//refresh gamePosition
			trace("target name@tryMovement:",target.pieceVO.name);
			trace("target original position@tryMovement:",target.pieceVO.position[0],target.pieceVO.position[1]);
			
			trace("before movement@tryMovement:",BoardModel.getInstance().gamePosition.dump());
			
			var pbArrayCollection:ArrayCollection = BoardModel.getInstance().gamePosition.gett(target.pieceVO.position[0],target.pieceVO.position[1]) as ArrayCollection;
			pbArrayCollection.removeItemAt(pbArrayCollection.getItemIndex(target));
			if( pbArrayCollection.length==0 )
			{
				BoardModel.getInstance().gamePosition.sett(target.pieceVO.position[0],target.pieceVO.position[1],null);
			}else
			{
				BoardModel.getInstance().gamePosition.sett(target.pieceVO.position[0],target.pieceVO.position[1],pbArrayCollection);
			}
			//anylize whether one dog(bluePiece) can be killed.
			if(!PositionTransactUtil.doesWalkable2positionsIsLinkable(target.pieceVO.position,newPosition))
			{
				//find out this could be killed dog(bluePieceButton):
				var dogsInTheWay:ArrayCollection = PositionTransactUtil.findDogsInTheWay(target.pieceVO.position,newPosition);
				trace("!!!find out this could be killed dog(bluePieceButton)!!!:",dogsInTheWay);
				//execute willBeKilled func;
				var dogInTheTopOfDogsInTheWay:PieceButton = dogsInTheWay.getItemAt(dogsInTheWay.length-1) as PieceButton;
				var executeKillVO:PieceVO = new PieceVO(true,dogInTheTopOfDogsInTheWay.pieceVO.name,dogInTheTopOfDogsInTheWay.pieceVO.position);
				dogInTheTopOfDogsInTheWay.pieceVO = executeKillVO;
				//refresh relative data;
				dogsInTheWay.removeItemAt(dogsInTheWay.length-1);
				if( dogsInTheWay.length==0 )
				{
					BoardModel.getInstance().gamePosition.sett(dogInTheTopOfDogsInTheWay.pieceVO.position[0],dogInTheTopOfDogsInTheWay.pieceVO.position[1],null);
				}else
				{
					BoardModel.getInstance().gamePosition.sett(dogInTheTopOfDogsInTheWay.pieceVO.position[0],dogInTheTopOfDogsInTheWay.pieceVO.position[1],dogsInTheWay);
				}
				BoardModel.getInstance().gamePosition.sett( dogInTheTopOfDogsInTheWay.pieceVO.position[0],
															dogInTheTopOfDogsInTheWay.pieceVO.position[1],
															dogsInTheWay );
				trace("after remove dogInTheTopOfDogsInTheWay:",BoardModel.getInstance().gamePosition.dump());											
			}
						
			//target movement;
			target.move( newDestination.x,newDestination.y);
			//			trace("target new point: ",Application.application.localToGlobal(new Point(target.x,target.y)).toString());
			trace("target new position@tryMovement:",newPosition.toString());
			
			//refresh new position;
			if(PiecesModel.getInstance().selectedPiece.pieceVO.name.indexOf(PieceButton.NAME_PREFIX_RED)!=-1)
			{
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(PiecesModel.getInstance().redPiecesCollection.getItemIndex(target)) as PieceButton).pieceVO.position = newPosition;
			}else
			{
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(PiecesModel.getInstance().bluePiecesCollection.getItemIndex(target)) as PieceButton).pieceVO.position = newPosition;
			}
			//refresh gamePosition
			BoardModel.getInstance().gamePosition.sett(newPosition[0],newPosition[1],new ArrayCollection([target]));
			trace("after arrived@tryMovement:",BoardModel.getInstance().gamePosition.dump());
			//repopluate game board;
			BoardManager.repopulation();
			//calculateResult:
			calculateResult(BoardModel.getInstance().gamePosition);
			
			return true;
		}
		
		private static function calculateResult(gamePositon:Array2):void
		{
			if(!PlayerManager.analysisGamePhases(PiecesModel.getInstance().redPiecesCollection,PiecesModel.getInstance().bluePiecesCollection))
			{
				if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
				{
					PlayerManager.isComputerTurnNow();
				}else
				{
					PlayerManager.isHumanTurnNow();	
				}
			}
		}	
		
		
	}
}